<?php
/*********************************************
 *  CPG Dragonfly™ CMS
 *********************************************
	Copyright © since 2010 by CPG-Nuke Dev Team
	http://dragonflycms.org

	Dragonfly is released under the terms and conditions
	of the GNU GPL version 2 or any later version
*/

namespace Dragonfly\L10N;

class Admin
{
	# http://tools.ietf.org/html/rfc4918#section-9
	public
		$allowed_methods = array('GET','POST');

	public function GET()
	{
		\Dragonfly::getKernel()->L10N->load('dragonfly_l10n');
		if (isset($_GET['translate'])) {
			$this->viewTranslator($_GET['translate']);
		} else {
			$this->viewList();
		}
	}

	public function POST()
	{
		$K = \Dragonfly::getKernel();
		$SQL = $K->SQL;

		if (isset($_GET['translate'])) {
			$tbl = $SQL->TBL->l10n_translate;
			$lng = preg_replace('/[^a-z]/','_',$_GET['translate']);
			if (isset($_POST['add'])) {
				if (!empty($_POST['translate_add']['msg_id'])) {
					$tbl->insert(array(
						'msg_id' => $_POST['translate_add']['msg_id'],
						$lng => $_POST['translate_add']['value'],
					));
				}
			} else if (isset($_POST['delete'])) {
				if (!empty($_POST['del']) && is_array($_POST['del'])) {
					$keys = array();
					foreach ($_POST['del'] as $v) { $keys[] = $SQL->quote($v); }
					$tbl->delete('msg_id IN ('.implode(',',$keys).')');
				}
			} else {
				foreach ($_POST['translate'] as $msg_id => $value) {
					$tbl->update(
						array($lng => $value),
						array('msg_id'=>$msg_id)
					);
				}
			}
		}
		else
		{
			if (isset($_POST['l10n']['default'])) {
				$K->CFG->set('global', 'language', $_POST['l10n']['default']);
				$K->CFG->set('global', 'multilingual', count($_POST['l10n']['active']));
			}
			if (isset($_POST['l10n']['active'])) {
				$ids = implode(',',$_POST['l10n']['active']);
				$SQL->exec("UPDATE {$SQL->TBL->l10n} SET l10n_active=0 WHERE l10n_id NOT IN ({$ids})");
				$SQL->exec("UPDATE {$SQL->TBL->l10n} SET l10n_active=1 WHERE l10n_id IN ({$ids}) OR l10n_rfc1766=".$SQL->quote($K->CFG->global->language));
			}
			$K->CACHE->delete('l10n_active');
		}
		\URL::redirect($_SERVER['REQUEST_URI']);
	}

	protected function viewList()
	{
		$K = \Dragonfly::getKernel();
		$OUT = $K->OUT;
		$OUT->languages = self::getAvailableLanguages();
		$OUT->display('dragonfly/l10n/overview');
	}

	protected function viewTranslator($lng)
	{
		$K = \Dragonfly::getKernel();
		$OUT = $K->OUT;
		$SQL = $K->SQL;
		$OUT->languages = self::getAvailableLanguages();

		$lng = preg_replace('/[^a-z]/','_',$lng);
		$OUT->translations = $SQL->query("SELECT
			msg_id,
			en title,
			{$lng} value
		FROM {$SQL->TBL->l10n_translate}
		ORDER BY 1");

		if ('en' === $lng) {
			$OUT->display('dragonfly/l10n/manage-ids');
		} else {
			$OUT->display('dragonfly/l10n/translate');
		}
	}

	public static function getAvailableLanguages()
	{
		static $languages = array();
		if (!$languages) {
			$K = \Dragonfly::getKernel();
			foreach (\Dragonfly_L10N::getFullList() as $k => $v) {
				$v['default'] = ($v['dir'] === $K->CFG->global->language);
				$v['label']   = $K->L10N->getNameForDir($v['dir']);
				$languages[$v['label']] = $v;

				if (!$v['id']) {
					$info = parse_ini_file("includes/l10n/{$v['dir']}/l10n.ini");
					if ($info && $info['rfc1766']==$v['dir'])
					{
						$K->SQL->TBL->l10n->insert(array(
							'l10n_rfc1766'  => $info['rfc1766'],
							'l10n_active'   => 0,
							'l10n_iso639_1' => $info['iso639_1'],
							'l10n_iso639_2' => $info['iso639_2'],
						),'l10n_id');

						try {
							$lng = preg_replace('/[^a-z]/','_',$v['dir']);
							$K->SQL->exec("ALTER TABLE {$K->SQL->TBL->l10n_translate} ADD COLUMN {$lng} text NULL");
						} catch (\Exception $e) {}
						$K->SQL->TBL->l10n->insert(array(
							'msg_id' => $info['rfc1766'],
							'en'     => $info['en'],
							$info['rfc1766'] => $info[$info['rfc1766']],
						),'l10n_id');
					}
				}
			}
			ksort($languages);
		}
		return $languages;
	}
}
